package com.mytest;

import android.app.Activity;
import android.os.Bundle;
import android.support.animation.DynamicAnimation;
import android.support.animation.SpringAnimation;
import android.support.animation.SpringForce;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.transitionseverywhere.ChangeBounds;
import com.transitionseverywhere.Transition;
import com.transitionseverywhere.TransitionManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TransActivity extends Activity {
    public static final String TAG = "TransActivity";

    private ImageView icon;
    @BindView(R.id.rl)
    RelativeLayout rl;
    private SpringForce springForce;


    @OnClick(R.id.btn_TRANSLATION_X)
    void btn_TRANSLATION_X() {
        final SpringAnimation animX = new SpringAnimation(icon, SpringAnimation.TRANSLATION_X).setSpring(springForce);
        animX.setStartValue(-10);//起始位置
        animX.start();
        final SpringAnimation animY = new SpringAnimation(icon, SpringAnimation.TRANSLATION_Y).setSpring(springForce);
        animY.setStartValue(-10);//起始位置
        animY.start();
    }

    @OnClick(R.id.btn_TRANSLATION_Y)
    void btn_TRANSLATION_Y() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.TRANSLATION_Y).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_TRANSLATION_Z)
    void btn_TRANSLATION_Z() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.TRANSLATION_Z).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_SCALE_X)
    void btn_SCALE_X() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.SCALE_X).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_SCALE_Y)
    void btn_SCALE_Y() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.SCALE_Y).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_ROTATION)
    void btn_ROTATION() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.ROTATION).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_ROTATION_X)
    void btn_ROTATION_X() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.ROTATION_X).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_ROTATION_Y)
    void btn_ROTATION_Y() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.ROTATION_Y).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_X)
    void btn_X() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.X).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_Y)
    void btn_Y() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.Y).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_Z)
    void btn_Z() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.Z).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_ALPHA)
    void btn_ALPHA() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.ALPHA).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_SCROLL_X)
    void btn_SCROLL_X() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.SCROLL_X).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    @OnClick(R.id.btn_SCROLL_Y)
    void btn_SCROLL_Y() {
        final SpringAnimation anim = new SpringAnimation(icon, SpringAnimation.SCROLL_Y).setSpring(springForce);
        anim.cancel();
        anim.setStartValue(50);//起始位置
        anim.start();
    }

    boolean isBottom = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trans);
        ButterKnife.bind(this);
        final ViewGroup transitionsContainer = (ViewGroup) findViewById(R.id.rl);


        icon = (ImageView) findViewById(R.id.icon);
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Transition transition = new ChangeBounds();
                transition.addListener(new Transition.TransitionListener() {
                    @Override
                    public void onTransitionStart(Transition transition) {
                        Log.e(TAG, "onTransitionStart: ");
                    }

                    @Override
                    public void onTransitionEnd(Transition transition) {

                        if (isBottom) {
                            Log.e(TAG, "onTransitionEnd: ");
                            final SpringAnimation animX = new SpringAnimation(icon, SpringAnimation.TRANSLATION_X).setSpring(springForce);
                            animX.setStartValue(-20);//起始位置
                            animX.start();
                            final SpringAnimation animY = new SpringAnimation(icon, SpringAnimation.TRANSLATION_Y).setSpring(springForce);
                            animY.setStartValue(-20);//起始位置
                            animY.start();
                        } else {
                            Log.e(TAG, "onTransitionEnd: ");
                            final SpringAnimation animX = new SpringAnimation(icon, SpringAnimation.TRANSLATION_X).setSpring(springForce);
                            animX.setStartValue(20);//起始位置
                            animX.start();
                            final SpringAnimation animY = new SpringAnimation(icon, SpringAnimation.TRANSLATION_Y).setSpring(springForce);
                            animY.setStartValue(20);//起始位置
                            animY.start();
                        }


                    }

                    @Override
                    public void onTransitionCancel(Transition transition) {

                    }

                    @Override
                    public void onTransitionPause(Transition transition) {

                    }

                    @Override
                    public void onTransitionResume(Transition transition) {

                    }
                });
                transition.setDuration(150);
                transition.setInterpolator(new AccelerateInterpolator());
                TransitionManager.beginDelayedTransition(transitionsContainer, transition);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
                if (isBottom) {
                    params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                } else {
                    params.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
                }
                icon.setLayoutParams(params);
                isBottom = !isBottom;


//                icon.animate()
//                        .y(100 + 200)
//                        .setDuration(0)
//                        .start();
            }
        });

        springForce = new SpringForce(0)//0 表示被作用对象的最终位置
                .setDampingRatio(0.8f)//设置弹性阻尼，dampingRatio越大，摆动次数越少，当到1的时候完全不摆动
                .setStiffness(SpringForce.STIFFNESS_LOW);//设置弹性的生硬度，stiffness值越小，弹簧越容易摆动，摆动的时间越长
    }
}
